package baidu.com.matedemo.pair;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.baidu.duer.libcore.util.ToastUtil;
import com.baidu.duer.smartmate.duerlink.bean.DuerlinkError;
import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.out.DuerSDK;
import com.baidu.duer.smartmate.out.oauth.IResponseCallback;
import com.baidu.duer.smartmate.protocol.dlp.bean.autentication.DeviceCodePairReturnPayload;
import com.baidu.duer.smartmate.proxy.IConnectionListener;
import com.baidu.duer.smartmate.proxy.bean.AuthenticationMessage;
import com.baidu.duer.smartmate.proxy.controller.AuthenticationObserver;

/**
 * Created by sunjian11 on 18/3/1.
 */

public class PrivilegePairV2 implements IDevicePair, IConnectionListener, AuthenticationObserver {
    private static final int PASSPORT_PAIR_TIMEOUT = 30 * 1000;

    private Activity mActivity = null;
    private DuerDevice mDuerDevice = null;
    private IResponseCallback mCallback = null;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    private Runnable mPassportPairTimeoutRunnable = new Runnable() {
        @Override
        public void run() {
            mCallback.onError(DuerlinkError.DLP_PASSPORT_PAIR_TIMEOUT.ordinal(),
                        DuerlinkError.DLP_PASSPORT_PAIR_TIMEOUT.toString());
            stopPair();
        }
    };

    @Override
    public void startPair(final Activity activity,
                          final DuerDevice duerDevice,
                          final IResponseCallback callback) {
        if (activity == null || duerDevice == null || callback == null) {
            return;
        }
        if (!DuerSDK.isLogin()) {
            ToastUtil.toastWarnning(activity, "请先登录");
            return;
        }
        mActivity = activity;
        mDuerDevice = duerDevice;
        mCallback = callback;
        mDuerDevice.getControllerManager().registerAuthenticationObserver(this);
        mDuerDevice.connect(mActivity, this);
    }

    @Override
    public void stopPair() {
        mHandler.removeCallbacks(mPassportPairTimeoutRunnable);
        if (mDuerDevice != null) {
            mDuerDevice.getControllerManager().unregisterAuthenticationObserver(this);
            mDuerDevice.unregisterConnectionListener(this);
            mDuerDevice = null;
        }
    }

    @Override
    public void onConnected() {
    }

    @Override
    public void onLocalConnected() {
        mDuerDevice.privilegeOauth(mActivity, new IResponseCallback() {
            @Override
            public void onSuccess() {
                mDuerDevice.getControllerManager().passportPair();
                mHandler.postDelayed(mPassportPairTimeoutRunnable, PASSPORT_PAIR_TIMEOUT);
            }

            @Override
            public void onError(long status, String msg) {
                mCallback.onError(DuerlinkError.OAUTH_FAIL.ordinal(),
                        DuerlinkError.OAUTH_FAIL.toString());
                stopPair();
            }
        });
    }

    @Override
    public void onConnectionFailed() {
        mCallback.onError(DuerlinkError.DLP_CONNECT_FAIL.ordinal(),
                DuerlinkError.DLP_CONNECT_FAIL.toString());
        stopPair();
    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onDataChanaged(String event, AuthenticationMessage message) {
        if ("PassportPairReturn".equals(event)) {
            mHandler.removeCallbacks(mPassportPairTimeoutRunnable);
            if (message != null && message.getStatus() == 0) {
                mCallback.onSuccess();
            } else {
                mCallback.onError(DuerlinkError.DLP_PASSPORT_PAIR_FAIL.ordinal(),
                            DuerlinkError.DLP_PASSPORT_PAIR_FAIL.toString());
            }
            stopPair();
        }
    }

    @Override
    public void onDeviceCodePairReturn(DeviceCodePairReturnPayload payload) {

    }
}
